package dca.sistemascontrole.view.components;

import java.awt.Color;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;

/**
 * Componente responsável por implementar um JButton Flat.
 *
 * @author Danilo Curvelo
 */
public class FlatButton extends JButton {

    private Border margin = new EmptyBorder(5, 10, 5, 10);
    private Color borderLine = new Color(200, 200, 200);
    private Color borderLineEntered = new Color(150, 150, 150);

    public FlatButton() {
        super();
        setFlatLayout();
        addMouseListeners();
    }

    public FlatButton(String text) {
        super(text);
        setFlatLayout();
        addMouseListeners();
    }

    public FlatButton(String text, Icon icon) {
        super(text, icon);
        setFlatLayout();
        addMouseListeners();
    }

    /**
     * Adiciona o layout flat ao JButton.
     */
    private void setFlatLayout() {
        setForeground(Color.BLACK);
        setBackground(Color.WHITE);
        Border line = new LineBorder(borderLine);
        Border compound = new CompoundBorder(line, margin);
        setBorder(compound);
        setContentAreaFilled(false);
        setFocusPainted(false);
        setOpaque(true);
    }

    /**
     * Adiciona os eventos de mouse ao JButton.
     */
    private void addMouseListeners() {
        this.addMouseListener(new java.awt.event.MouseAdapter() {
            @Override
            public void mousePressed(java.awt.event.MouseEvent evt) {
                mousePressedFlatButton(evt);
            }

            @Override
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                mouseReleasedFlatButton(evt);
            }

            @Override
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                mouseEnteredFlatButton(evt);
            }

            @Override
            public void mouseExited(java.awt.event.MouseEvent evt) {
                mouseExitedFlatButton(evt);
            }
        });
    }

    private void mousePressedFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            setForeground(Color.GRAY);
            Border line = new LineBorder(Color.GRAY);
            Border compound = new CompoundBorder(line, margin);
            setBorder(compound);
        }
    }

    private void mouseReleasedFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            setForeground(Color.BLACK);
            Border line = new LineBorder(borderLineEntered);
            Border compound = new CompoundBorder(line, margin);
            setBorder(compound);
        }
    }

    private void mouseEnteredFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            setForeground(Color.BLACK);
            Border line = new LineBorder(borderLineEntered);
            Border compound = new CompoundBorder(line, margin);
            setBorder(compound);
        }
    }

    private void mouseExitedFlatButton(java.awt.event.MouseEvent evt) {
        if (isEnabled()) {
            setForeground(Color.BLACK);
            Border line = new LineBorder(borderLine);
            Border compound = new CompoundBorder(line, margin);
            setBorder(compound);
        }
    }
}
